<template>
  <div>
    <el-card class="login-card">
      <div slot="header" class="clearfix">
        <span>请先登录</span>
        <router-link tag="a" to="/register" class="to-register">转到注册
        </router-link>
      </div>
      <el-form @submit.native.prevent="login">
        <el-form-item label="用户名">
          <el-input v-model="model.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="model.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      model: {}
    }
  },
  methods: {
    async login () {
      const res = await this.$http.post('login', this.model)
      localStorage.token = res.data.token
      this.$router.push('/')
      this.$message({ type: 'success', message: '登录成功' })
    }
  }
}
</script>

<style scoped>
.login-card {
  width: 30rem;
  margin: 10rem auto;
}
.to-register {
  float: right;
  padding: 3px 0;
  text-decoration: none;
  color: #008080;
}
</style>
